package org.urey.fmp.consumer.processor;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import feign.Param.Expander;

@Component
public class CustomJsonExpander implements Expander {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ObjectMapper objectMapper;

	@Override
	public String expand(Object value) {
		try {
			String s = null;
			if (value instanceof Date) {
				s = ((Date) value).getTime() + "";
			} else {
				s = objectMapper.writeValueAsString(value);
			}
			logger.debug("feign expander:{}:{}", value.toString(), s);
			return s;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return "";
		}
	}

}
